package com.tengju.data.infrastructure.repository.clickhouse.convert;

import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.model.user.UserBelongDetail;
import com.tengju.data.domain.model.user.UserDetail;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.UserBelongDetailDO;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.UserDetailDO;
import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.model.user.UserIdCode;

import java.time.ZoneId;
import java.util.Optional;

public class OdsUserInfoWithClubConvert {

    public static UserDetail convertToModel(UserDetailDO userDetailDO) {
        if (userDetailDO == null) {
            return null;
        }
        return new UserDetail(new UserIdCode(userDetailDO.getIdCode()),
                userDetailDO.getAreaCode(),
                userDetailDO.getMobile(),
                StarLevelEnum.parseByStarLevel(userDetailDO.getStarLevel()),
                userDetailDO.getName(),
                userDetailDO.getNickname(),
                userDetailDO.getCreateTime() == null ?
                        null : userDetailDO.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
                Optional.ofNullable(userDetailDO.getParentIdCode())
                        .filter(parentIdCode -> parentIdCode > 0).map(UserIdCode::new).orElse(null),
                StarLevelEnum.parseByStarLevel(userDetailDO.getParentStarLevel()),
                Optional.ofNullable(userDetailDO.getCityIdCode())
                        .filter(city -> city > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongEC())
                        .filter(belongEc -> belongEc > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongSEC())
                        .filter(belongSec -> belongSec > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongEEC())
                        .filter(belongEec -> belongEec > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getGenSecIdCode())
                        .filter(genSec -> genSec > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongClubId())
                        .filter(clubId -> clubId > 0).map(ClubId::new).orElse(null),
                null,
                userDetailDO.getUpgradeBlackCardTime() == null ?
                        null : userDetailDO.getUpgradeBlackCardTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
                Optional.ofNullable(userDetailDO.getGenOperatorIdCode())
                        .filter(genOperator -> genOperator > 0).map(UserIdCode::new).orElse(null));
    }

    public static UserBelongDetail convertToModel(UserBelongDetailDO userDetailDO) {
        if (userDetailDO == null) {
            return null;
        }
        return new UserBelongDetail(new UserIdCode(userDetailDO.getIdCode()),
                Optional.ofNullable(userDetailDO.getCityIdCode())
                        .filter(city -> city > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongEC())
                        .filter(belongEc -> belongEc > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongSEC())
                        .filter(belongSec -> belongSec > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongEEC())
                        .filter(belongEec -> belongEec > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getGenSecIdCode())
                        .filter(genSec -> genSec > 0).map(UserIdCode::new).orElse(null),
                Optional.ofNullable(userDetailDO.getBelongClubId())
                        .filter(clubId -> clubId > 0).map(ClubId::new).orElse(null),
                userDetailDO.getUpgradeBlackCardTime() == null ?
                        null : userDetailDO.getUpgradeBlackCardTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
                Optional.ofNullable(userDetailDO.getGenOperatorIdCode())
                        .filter(genOperator -> genOperator > 0).map(UserIdCode::new).orElse(null));
    }

}
